"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createRoute = void 0;
const koa_1 = __importDefault(require("koa"));
const koa_router_1 = __importDefault(require("koa-router"));
function createRoute(port, col) {
    return __awaiter(this, void 0, void 0, function* () {
        const app = new koa_1.default(); // 创建koa应用
        const router = new koa_router_1.default(); // 创建路由，支持传递参数
        // 指定一个url匹配
        router.get('/:m', (ctx) => __awaiter(this, void 0, void 0, function* () {
            if (ctx.params.m !== 'Yuisean') {
                ctx.status = 400;
                return;
            }
            const arr = yield col.find().sort({ "tier": 1 }).toArray();
            ctx.type = 'application/json;charset=utf-8';
            let i = 0;
            const top = [];
            const jug = [];
            const mid = [];
            const adc = [];
            const sup = [];
            for (const item of arr) {
                item.name_zh = item.name_zh.split(":")[0];
                if (item.position === 'top') {
                    top.push(item);
                }
                else if (item.position == 'jug') {
                    jug.push(item);
                }
                else if (item.position == 'mid') {
                    mid.push(item);
                }
                else if (item.position == 'adc') {
                    adc.push(item);
                }
                else {
                    sup.push(item);
                }
            }
            ctx.body = JSON.stringify([top, jug, mid, adc, sup]);
        }));
        // 调用router.routes()来组装匹配好的路由，返回一个合并好的中间件
        // 调用router.allowedMethods()获得一个中间件，当发送了不符合的请求时，会返回 `405 Method Not Allowed` 或 `501 Not Implemented`
        app.use(router.routes());
        app.use(router.allowedMethods({
        // throw: true, // 抛出错误，代替设置响应头状态
        // notImplemented: () => '不支持当前请求所需要的功能',
        // methodNotAllowed: () => '不支持的请求方式'
        }));
        app.listen(port);
    });
}
exports.createRoute = createRoute;
